//pretested


#include "list.h"
#ifdef __cplusplus
#define NULL	nullptr
#else
#define NULL    ((void*)0)
#endif
template<int N>
void InsertListNode(listnode<N>* list,listnode<N>* tnode)//Problem
{
    if(list == NULL) return;
    if(list->next != NULL){
        tnode->next = list->next;

        tnode->pre = list;
        tnode->next->pre = tnode;
        tnode->pre->next = tnode;}
        else
        {
            list->next = tnode;
            tnode->pre = list;
        }
}

template<int N>
void CombineList(listnode<N>* first,listnode<N>* second)
{
    while(first->next != NULL)
    {
        first = first->next;
    }
    while(second->pre != NULL)
    {

        second = second->pre;
    }
    first->next = second;
    second->pre = first;
}
template<int N>
void RemoveListNode(listnode<N>* tnode)
{
    if(tnode->pre != NULL)
        tnode->pre->next = tnode->next;
    if(tnode->next != NULL)
        tnode->next->pre = tnode->pre;
    tnode->pre = NULL;
    tnode->next = NULL;
}
template<int N>
listnode<N>* MakeListNode(listnode<N>* newnode,void* mark)
{
    newnode->pre = NULL;
    newnode->next = NULL;
    newnode->mark[0] = mark;
    return newnode;
}
template<int N>
void UpListNode(listnode<N>* tnode)
{
    if(tnode->pre == NULL) return;
    listnode* pointer = tnode;
    while(pointer->pre != NULL)
    {
       pointer = pointer->pre;
    }
    RemoveListNode(tnode);
    InsertListNode(pointer,tnode);
}

slnode* MakeSListNode(slnode* newnode,void* mark)
{
    newnode->mark = mark;
    newnode->next = NULL;
}

void InsertSListNode(slnode* list,slnode* tnode)
{
       if(list == NULL) return;
        if(list->next != NULL){
        tnode->next = list->next;
        }

            list->next = tnode;

}

void CombineSList(slnode* first,slnode* second)
{
    while(first->next != NULL)
    {
        first = first->next;
    }
    first->next = second;
}

int RemoveSListNode(slnode* from,slnode* tnode)
{

    while(from->next != tnode && from->next != NULL)
    {
        from = from->next;
    }
    if(from->next != tnode) return 0;
    from->next = tnode->next;
    tnode->next = NULL;
    return 1;
}
